<template>
  <div class="warehouse-maintenance-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>物料仓库基础维护</span>
          <el-button type="primary" @click="handleAdd">新增仓库</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="仓库编码">
          <el-input v-model="queryForm.code" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="queryForm.name" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="仓库类型">
          <el-select v-model="queryForm.type" placeholder="请选择仓库类型">
            <el-option label="原材料仓" value="raw" />
            <el-option label="成品仓" value="finished" />
            <el-option label="半成品仓" value="semi" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="code" label="仓库编码" />
        <el-table-column prop="name" label="仓库名称" />
        <el-table-column prop="type" label="仓库类型">
          <template #default="scope">
            {{ warehouseTypes[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="manager" label="负责人" />
        <el-table-column prop="contact" label="联系方式" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
              {{ scope.row.status === 'active' ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button 
              link 
              :type="scope.row.status === 'active' ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 'active' ? '停用' : '启用' }}
            </el-button>
            <el-button link type="primary" @click="handleArea(scope.row)">库区管理</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 仓库表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增仓库' : '编辑仓库'"
      width="500px"
    >
      <el-form :model="warehouseForm" label-width="100px" :rules="rules" ref="warehouseFormRef">
        <el-form-item label="仓库编码" prop="code">
          <el-input v-model="warehouseForm.code" :disabled="dialogType === 'edit'" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="warehouseForm.name" />
        </el-form-item>
        <el-form-item label="仓库类型" prop="type">
          <el-select v-model="warehouseForm.type">
            <el-option label="原材料仓" value="raw" />
            <el-option label="成品仓" value="finished" />
            <el-option label="半成品仓" value="semi" />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="manager">
          <el-input v-model="warehouseForm.manager" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="warehouseForm.contact" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="warehouseForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 库区管理对话框 -->
    <el-dialog
      v-model="areaDialogVisible"
      title="库区管理"
      width="800px"
    >
      <el-table :data="areaData" border style="width: 100%">
        <el-table-column prop="code" label="库区编码" />
        <el-table-column prop="name" label="库区名称" />
        <el-table-column prop="capacity" label="容量" />
        <el-table-column prop="used" label="已使用" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button link type="primary" @click="handleEditArea(scope.row)">编辑</el-button>
            <el-button link type="danger" @click="handleDeleteArea(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleAddArea">新增库区</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const queryForm = reactive({
  code: '',
  name: '',
  type: ''
})

const warehouseTypes = {
  raw: '原材料仓',
  finished: '成品仓',
  semi: '半成品仓'
}

const tableData = ref([
  {
    code: 'WH001',
    name: '原材料仓库A',
    type: 'raw',
    manager: '张三',
    contact: '13800138000',
    status: 'active'
  }
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const dialogVisible = ref(false)
const dialogType = ref('add')
const warehouseForm = reactive({
  code: '',
  name: '',
  type: '',
  manager: '',
  contact: '',
  remark: ''
})

const rules = {
  code: [{ required: true, message: '请输入仓库编码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择仓库类型', trigger: 'change' }],
  manager: [{ required: true, message: '请输入负责人', trigger: 'blur' }],
  contact: [{ required: true, message: '请输入联系方式', trigger: 'blur' }]
}

const areaDialogVisible = ref(false)
const areaData = ref([
  {
    code: 'A001',
    name: 'A区',
    capacity: 1000,
    used: 300
  }
])

// 查询方法
const handleSearch = () => {
  // 实现查询逻辑
}

const handleReset = () => {
  queryForm.code = ''
  queryForm.name = ''
  queryForm.type = ''
}

// 仓库管理方法
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  Object.assign(warehouseForm, {
    code: '',
    name: '',
    type: '',
    manager: '',
    contact: '',
    remark: ''
  })
}

const handleEdit = (row) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  Object.assign(warehouseForm, row)
}

const handleSubmit = () => {
  // 实现提交逻辑
  ElMessage.success(dialogType.value === 'add' ? '新增成功' : '编辑成功')
  dialogVisible.value = false
}

const handleToggleStatus = (row) => {
  const action = row.status === 'active' ? '停用' : '启用'
  ElMessageBox.confirm(
    `确认${action}该仓库吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    row.status = row.status === 'active' ? 'inactive' : 'active'
    ElMessage.success(`${action}成功`)
  })
}

// 库区管理方法
const handleArea = (row) => {
  areaDialogVisible.value = true
  // 加载库区数据
}

const handleAddArea = () => {
  // 实现新增库区逻辑
}

const handleEditArea = (row) => {
  // 实现编辑库区逻辑
}

const handleDeleteArea = (row) => {
  ElMessageBox.confirm(
    '确认删除该库区吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
  })
}

// 分页方法
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.warehouse-maintenance-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  margin-top: 20px;
  text-align: right;
}
</style> 